package com.dhcc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhcc.SnowflakeIdWorker;
import com.dhcc.entity.SysUser;
import com.dhcc.mapper.SysUserMapper;
import com.dhcc.service.SysUserService;
import jakarta.annotation.Resource;
import lombok.val;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;

/**
 * 用户Service实现类
 *
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.service.impl
 * @Title SysUserServiceImpl
 * @Date 2024/10/11 下午3:41
 * @Author LiuGuoting
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;
    /**
     * 根据用户名查询用户信息
     *
     * @param username 用户名
     * @return SysUser 用户实体
     */
    @Override
    public SysUser getUserByUsername(String username) {
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(SysUser::getUserName, username);
        return this.getOne(sysUserLambdaQueryWrapper);
    }

    /**
     * 注册用户信息
     *
     * @param user 用户实体
     * @return 插入的行数
     */
    @Override
    public boolean registerUser(SysUser user) {
        user.setId(snowflakeIdWorker.nextId());
        user.setCreateUser(user.getUserName());
        user.setCreateTime(String.valueOf(LocalDateTime.now()));
        user.setUpdateUser(user.getUserName());
        user.setUpdateTime(String.valueOf(LocalDateTime.now()));
        user.setDelFlag((byte) 0);
        user.setStatus((byte) 1);
        return this.save(user);
    }
}
